package com.exce.vo;

import com.exce.enums.ErrorEnum;

/**
 * @author yinxing
 * @date 2022-09-09
 */
public class Response {

    private Boolean success;

    private Integer code;

    private String msg;

    private Object data;

    public Response() {

    }

    /**
     * 自定义返回结果的构造方法
     * @param success 成功标识
     * @param code 编码
     * @param msg 消息
     * @param data 数据
     */
    public Response(Boolean success, Integer code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 自定义异常返回的结果
     * @param de
     * @return
     */
    public static Response defineError(DiyException de){
        Response result = new Response();
        result.setSuccess(false);
        result.setCode(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        result.setData(null);
        return result;
    }

    /**
     * 其他异常处理方法返回的结果
     * @param errorEnum
     * @return
     */
    public static Response otherError(ErrorEnum errorEnum){
        Response result = new Response();
        result.setMsg(errorEnum.getErrorMsg());
        result.setCode(errorEnum.getErrorCode());
        result.setSuccess(false);
        result.setData(null);
        return result;
    }

    /**
     * 一般成功返回
     * @param msg
     * @param data
     * @return
     */
    public static Response success(String msg, Object data){
        Response result = new Response();
        result.setMsg(msg);
        result.setCode(ErrorEnum.SUCCESS.getErrorCode());
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    public Boolean getSuccess() {
        return success;
    }
    public void setSuccess(Boolean success) {
        this.success = success;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }

}
